﻿using fabao.Common.Util;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Data;
using System.Domain.Entities;

namespace tianzedianzi
{
    public static class LayHtmlHelper
    {
        /// <summary>
        /// 设置按钮
        /// </summary>
        /// <param name="list">当前权限按钮列表</param>
        /// <param name="btnCode">按钮英文标识</param>
        /// <param name="btnClassName">类名</param>
        /// <param name="extInfo">属性</param>
        /// <param name="icon">图标</param>
        /// <returns></returns>
        public static IHtmlContent SetPermissionButton(this IHtmlHelper htmlHelper, List<Menu> userButtons, string btnCode, string btnClassName, string extInfo = "", string icon = "")
        {
            string text = "";
            string btnName = "";
            string className = "";
            if (userButtons != null && userButtons.Count > 0)
            {
                bool flag = false;
                foreach (Menu menu in userButtons)
                {
                    if (menu.btnEnName == btnCode)
                    {
                        btnName = menu.btnName;
                        className = btnClassName;
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    return htmlHelper.LayUiButton(btnName, className, icon, extInfo);
                }
            }
            return new HtmlString(text);
        }
        /// <summary>
        /// layui 按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="btnName"></param>
        /// <param name="className"></param>
        /// <param name="icon"></param>
        /// <param name="extInfo"></param>
        /// <returns></returns>
        public static IHtmlContent LayUiButton(this IHtmlHelper htmlHelper, string btnName, string className, string icon = "", string extInfo = "")
        {
            string source = string.Concat(new string[]
            {
                "<button class=\"layui-btn ",
                className,
                " \" ",
                extInfo,
                ">pt_icon ",
                btnName,
                "</button>"
            });
            string newString = "";
            if (!string.IsNullOrEmpty(icon))
            {
                newString = "<i class=\"layui-icon " + icon + "\"></i>";
            }
            return new HtmlString(StringHelper.ReplaceAll(source, "pt_icon", newString));
        }
        public static string GetEvent(string eventName)
        {
            return "lay-event=\"" + eventName + "\"";
        }
    }
}
